package com.bw.shop2208a.detail

import android.animation.Animator
import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.app.ActionBar.LayoutParams
import android.content.Context
import android.net.Uri
import android.widget.ImageView
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.bumptech.glide.Glide
import com.bw.shop2208a.R
import com.bw.shop2208a.databinding.ActivityGoods2Binding
import com.bw.shop2208a.entity.AddcarParam
import com.bw.shop2208a.entity.DataGoodsEntity
import com.bw.shop2208a.mvvm.BaseActivity
import com.bw.shop2208a.mvvm.BaseViewActivity
import com.bw.shop2208a.utils.GlideUtils
import com.youth.banner.loader.ImageLoader
import com.youth.banner.loader.ImageLoaderInterface

@Suppress("DEPRECATION")
@Route(path = "/app/goods")
class DetailActivity : BaseActivity<ActivityGoods2Binding, DetailViewModel>() {
//    val list = mutableListOf<String>()
    @Autowired
    lateinit var goods:DataGoodsEntity
    override fun initViwe() {

        ARouter.getInstance().inject(this)
        viewModel.getDetail(goods.id)

        //点击购物车
        binding.tvAddCar.setOnClickListener {
            viewModel.addCar(AddcarParam(1,goods.id))
        }
    }

    override fun iniData() {
        viewModel.successDetail.observe(this){
            binding.goods = it.data
        }
        viewModel.failDetail.observe(this){
            println(it)
        }
        viewModel.successAddCar.observe(this){
            ToastUtils.showLong(it.message)
            if (it.code==200){
                val image = ImageView(this)
                val param = LayoutParams(200,200)
                image.layoutParams = param//图片的大小为200*200
                GlideUtils.loadImage(this,image,goods.goods_default_icon)
                binding.ll.addView(image)//将图片显示在布局上

                val height = windowManager.defaultDisplay.height
                val width = windowManager.defaultDisplay.width
                val translationX = ObjectAnimator.ofFloat(image,"translationX",0f,(width).toFloat())
                val translationY = ObjectAnimator.ofFloat(image,"translationY",0f,-(height).toFloat())
                val scaleX = ObjectAnimator.ofFloat(image, "scaleX", 2f, 0.5f)
                val scaleY = ObjectAnimator.ofFloat(image, "scaleY", 2f, 0.5f)
                val animatorSet = AnimatorSet()
                animatorSet.duration = 2000
                animatorSet.play(translationX).with(translationY).with(scaleX).with(scaleY)
                animatorSet.start()

                //消失
                animatorSet.addListener(object :Animator.AnimatorListener{
                    override fun onAnimationStart(animation: Animator) {

                    }

                    override fun onAnimationEnd(animation: Animator) {
                        binding.ll.removeView(image)
                        animatorSet.cancel()
                    }

                    override fun onAnimationCancel(animation: Animator) {

                    }

                    override fun onAnimationRepeat(animation: Animator) {

                    }
                })
            }
        }
    }

    override fun getLayoutId(): Int = R.layout.activity_goods2
    override fun getViewModel(): Class<DetailViewModel> = DetailViewModel::class.java

}